import {
  Toast
} from "mint-ui";
export default {
  install(Vue, options) {
    // 获取用户信息
    Vue.prototype.baseUserMessageDistributeFun = function (val) {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      if (isAndroid) {
        if (window.BenchJSInterface) {
          if (window.BenchJSInterface.getUserToken) {
            window.BenchJSInterface.getUserToken(
              "javascript:window.getAppgetUserToken(->path<-);"
            );
          }
        }
      } else {
        let setUserObj = {
          methodName: "BotongApplicationCenterUserInfoJSHandler",
          params: {},
          callbackMethod: "getApplicationRelevantInfoFun"
        };
        if (window.webkit) {
          if (window.webkit.messageHandlers) {
            if (
              window.webkit.messageHandlers
              .BotongApplicationCenterUserInfoJSHandler
            ) {
              window.webkit.messageHandlers.BotongApplicationCenterUserInfoJSHandler.postMessage(
                setUserObj
              );
            }
          }
        }
      }
    };
    Vue.prototype.baseUserInfoFun = function (val) {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      let userobj = JSON.parse(JSON.stringify(val));
      let user = {};
      if (isAndroid) {
        user = {
          token: userobj.token,
          userOrgList: userobj.userOrgList,
        };
      } else {
        user = {
          token: userobj.token,
          userOrgList: userobj.userOrgList,
        };
      }
      sessionStorage.setItem("TOKEN", user.token);
      sessionStorage.setItem("USERORGLIST", JSON.stringify(user.userOrgList));
    };
    // 获取组织机构
    Vue.prototype.baseUserchooseOrgFun = function (params, str) {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      if (isAndroid) {
        if (window.BenchJSInterface) {
          if (window.BenchJSInterface.chooseOrg) {
            window.BenchJSInterface.chooseOrg(
              params,
              `javascript:window.${str}(->path<-);`
            );
          }
        }
      } else {
        let setUserObj = {
          methodName: "BotongApplicationCenterUserInfoJSHandler",
          params: {},
          callbackMethod: "getApplicationRelevantInfoFun"
        };
        if (window.webkit) {
          if (window.webkit.messageHandlers) {
            if (
              window.webkit.messageHandlers
              .BotongApplicationCenterUserInfoJSHandler
            ) {
              window.webkit.messageHandlers.BotongApplicationCenterUserInfoJSHandler.postMessage(
                setUserObj
              );
            }
          }
        }
      }
    };
    Vue.prototype.basechooseOrgInfoFun = function (val) {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      let userobj = JSON.parse(JSON.stringify(val));
      if (isAndroid) {
        return userobj
      } else {
        user = {
          token: userobj.token,
          userOrgList: userobj.userOrgList,
        };
      }
    };
    // 图片预览
    Vue.prototype.WatchBigPicture = function (m, n) {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
      if (isAndroid) {
        if (window.BenchJSInterface) {
          if (window.BenchJSInterface.WatchBigPicture) {
            let obj = {
              imgLists: m,
              index: n
            };
            window.BenchJSInterface.WatchBigPicture(JSON.stringify(obj));
          }
        }
      } else {
        let messgaeObjImg = {
          methodName: "invokeCameraFromJS",
          params: {
            imageArray: m,
            currentCount: n
          },
          callbackMethod: "updateIosImgFun"
        };
        if (window.webkit) {
          if (window.webkit.messageHandlers) {
            if (
              window.webkit.messageHandlers
              .BotongApplicationCenterPictureBrowserJSHandler
            ) {
              window.webkit.messageHandlers.BotongApplicationCenterPictureBrowserJSHandler.postMessage(
                messgaeObjImg
              );
            }
          }
        }
      }
    };
    // 退出
    Vue.prototype.baseBackFun = function () {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      if (isAndroid) {
        if (window.BenchJSInterface) {
          window.BenchJSInterface.finishWebActivity();
        }
      } else {
        let setNav = {
          methodName: "invokeNavigationPopToRoot"
        };
        if (window.webkit) {
          if (window.webkit.messageHandlers) {
            if (
              window.webkit.messageHandlers
              .BotongApplicationCenterNavigationJSHandler
            ) {
              window.webkit.messageHandlers.BotongApplicationCenterNavigationJSHandler.postMessage(
                setNav
              );
            }
          }
        }
      }
    };
    // Tost
    Vue.prototype.baseToastFun = function (val = "服务器异常") {
      Toast({
        message: val,
        position: "bottom"
      });
    };
    // 跳转route
    Vue.prototype.basePushFun = function (val = "/") {
      this.$router.push({
        path: val
      });
    };
    // 设置标题
    /**
     *title : 导航名称
     */
    Vue.prototype.titleFun = function (M) {
      document.title = M.title || "";
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      if (isAndroid) {
        if (window.BenchJSInterface) {
          if (window.BenchJSInterface.setTitle) {
            window.BenchJSInterface.setTitle(M.title || "");
          }
        }
      } else {
        
      }
    };
    // 选择
    Vue.prototype.userListFun = function (M) {
      let u = navigator.userAgent;
      let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
      if (isAndroid) {
        if (window.BenchJSInterface) {
          if (M.lists.length > 0) {
            window.BenchJSInterface.setLargeMaxMembers(
              "101",
              `javascript:window.${M.path}(->path<-);`,
              JSON.stringify(M.lists),
              M.limitCount,
              M.canSelectSelf || 0
            );
          } else {
            window.BenchJSInterface.setLargeMaxMembers(
              "101",
              `javascript:window.${M.path}(->path<-);`,
              M.limitCount,
              M.canSelectSelf || 0
            );
          }
        }
      } else {
        let messgaeObj = {};
        if (M.lists.length > 0) {
          let arrSelects = [];
          if (M.lists.length > 0) {
            M.lists.forEach(function (ele) {
              arrSelects.push(ele.passportId);
            });
          }
          messgaeObj = {
            methodName: "BotongApplicationCenterContactsJSHandler",
            params: {
              selected: JSON.stringify(M.lists),
              limitCount: M.limitCount,
              canSelectSelf: M.canSelectSelf || 0
            },
            callbackMethod: `${M.path}`
          };
        } else {
          messgaeObj = {
            methodName: "BotongApplicationCenterContactsJSHandler",
            params: {
              selected: "",
              limitCount: M.limitCount,
              canSelectSelf: M.canSelectSelf || 0
            },
            callbackMethod: `${M.path}`
          };
        }

        if (window.webkit) {
          window.webkit.messageHandlers.BotongApplicationCenterContactsJSHandler.postMessage(
            messgaeObj
          );
        }
      }
    };
    // 
    Vue.prototype.baseDateFun = function (val) {
      if (val) {
        var standardTime = new Date(Number(val));
        var y, m, d, h, f;
        y = standardTime.getFullYear();
        m = standardTime.getMonth() + 1;
        m = m > 9 ? m : "0" + m;
        d = standardTime.getDate();
        d = d > 9 ? d : "0" + d;
        h = standardTime.getHours();
        h = h > 9 ? h : "0" + h;
        f = standardTime.getMinutes();
        f = f > 9 ? f : "0" + f;
        return y + "-" + m + "-" + d;
      } else {
        return "暂无";
      }
    };
    // 
    Vue.prototype.baseDateTjFun = function (val) {
      if (val) {
        var standardTime = new Date(Number(val));
        var y, m, d, h, f;
        y = standardTime.getFullYear();
        m = standardTime.getMonth() + 1;
        m = m > 9 ? m : "0" + m;
        d = standardTime.getDate();
        d = d > 9 ? d : "0" + d;
        h = standardTime.getHours();
        h = h > 9 ? h : "0" + h;
        f = standardTime.getMinutes();
        f = f > 9 ? f : "0" + f;
        return y + "-" + m
      } else {
        return "暂无";
      }
    };
  }
};
